<template>
  <div style="margin-bottom: 32px">
    <el-form label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="能力名称">
            <span>{{ fbOptions.formData.serviceName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="能力编码">
            <span>{{ fbOptions.formData.serviceCode }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="能力类型">
            <span>{{
                selectDictLabel(fbOptions.protocolTypeList, fbOptions.formData.serviceType)
              }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请求路径">
            <span>{{ fbOptions.formData.mockUrl }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="能力描述">
            <span>{{ fbOptions.formData.serviceDesc }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table style="margin: 10px 0" :data="versionList">
      <el-table-column label="版本号" prop="serviceVerId" width="80">
        <template #default="scope">
          <div>
            <span>{{
                scope.row.serviceVersion ? `V${scope.row.serviceVersion}` : ""
              }}</span>
            <el-tag
              size="mini"
              v-if="fbOptions.formData.serviceMasterVersion === scope.row.serviceVersion"
            >主
            </el-tag
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="版本描述"
        prop="serviceVerDesc"
        :show-overflow-tooltip="true"
        min-width="100"
      />
      <el-table-column
        label="请求方式"
        prop="httpRequestMethod"
        width="80"
        align="center"
      />
      <el-table-column label="状态" align="center" width="80">
        <template #default="scope">
          <status-tag :status="scope.row.serviceVerStatus"/>
        </template>
      </el-table-column>
      <el-table-column
        label="更新者"
        prop="updateBy"
        :show-overflow-tooltip="true"
        min-width="60"
      />
      <el-table-column label="更新时间" prop="updateTime" width="150"/>
      <el-table-column label="操作" min-width="200">
        <template #default="scope">
          <text-detail-button
            @click="handleAction('view', scope.row)"
            text="详情"
          />
          <!-- <text-detail-button
              @click="handleAction('test', scope.row)"
              text="测试"
            /> -->
          <text-edit-button
            v-if="actionBtnHandler(scope.row.serviceVerStatus)"
            @click="handleAction('edit', scope.row)"
            text="修改"
          />
          <text-release-button
            v-if="
              scope.row.serviceVerStatus === '1' ||
              scope.row.serviceVerStatus === '2'
            "
            :title="`是否确认发布版本V${scope.row.serviceVersion}?`"
            @click="handleAction('release', scope.row)"
          />
          <text-revoke-button
            v-if="scope.row.serviceVerStatus === '2'"
            text="作废"
            :title="`是否确认作废版本V${scope.row.serviceVersion}?`"
            @click="handleAction('discard', scope.row)"
          />
          <text-revoke-button
            v-if="scope.row.serviceVerStatus === '0'"
            text="撤销"
            :title="`是否确认撤销版本V${scope.row.serviceVersion}?`"
            @click="handleAction('backout', scope.row)"
          />
          <text-disable-button
            v-if="
              scope.row.serviceVerStatus === '0' &&
              fbOptions.formData.serviceMasterVersion !== scope.row.serviceVersion
            "
            text="设为主版本"
            :title="`是否确认将V${scope.row.serviceVersion}设为主版本?`"
            @click="handleAction('setmain', scope.row)"
          />
          <text-detail-button
            v-if="
              scope.row.serviceVerStatus === '0' ||
              scope.row.serviceVerStatus === '2'
            "
            @click="handleAction('upgrade', scope.row)"
            text="升级"
          />
          <text-detail-button
            v-if="scope.row.serviceVerStatus === '0'"
            @click="handleAction('reload', scope.row)"
            text="刷新路由"
          />
          <text-delete-button
            v-if="
              scope.row.serviceVerStatus === '1' ||
              scope.row.serviceVerStatus === '9'
            "
            :title="`是否确认删除版本V${scope.row.serviceVersion}?`"
            @click="handleAction('delete', scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getVersionList"
    />
  </div>
</template>

<script>
import {
  searchVersion,
  reloadRouter,
  revokeService,
  publishService,
  deleteServiceVersion,
  setMasterVersion,
  discardService
} from "@slc/api/slc/inventory";
import StatusTag from "./status-tag.vue";

export default {
  components: {
    StatusTag,
  },
  props: {
    fbOptions: {
      type: Object,
      default: () => {
      },
    },
  },
  data() {
    return {
      versionList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  watch: {
    'fbOptions.isVisible': {
      handler(val) {
        if (val) {
          this.getVersionList();
        }
      },
      immediate: true,
    },
  },
  computed: {},
  methods: {
    async getVersionList() {
      this.versionList = [];
      const res = await searchVersion({
        ...this.queryParams,
        serviceId: this.fbOptions.formData.serviceId,
      });
      this.versionList = res.data.list || [];
      this.total = res.data.total;
    },
    async handleAction(key, row) {
      row.isFlbp = true;
      if (key === "reload") {
        // 直接刷新单台，后台多服务直接通过分布式协调器同步
        const res = await reloadRouter({serviceVerId: row.serviceVerId});
        if (res.code === "200") {
          this.$message.success("刷新成功！");
          this.getVersionList();
        }
      } else if (key === "backout") {
        const res = await revokeService({
          serviceId: row.serviceId,
          serviceVersion: row.serviceVersion,
        });
        if (res.code === "200") {
          this.$message.success("撤销成功！");
          this.getVersionList();
        }
      } else if (key === "release") {
        const res = await publishService({serviceVerId: row.serviceVerId});
        if (res.code === "200") {
          this.$message.success("发布成功！");
          this.getVersionList();
        }
      } else if (key === "delete") {
        const res = await deleteServiceVersion({
          serviceId: row.serviceId,
          serviceVersion: row.serviceVersion,
        });
        if (res.code === "200") {
          this.$message.success("删除成功！");
          this.getVersionList();
        }
      } else if (key === "setmain") {
        const res = await setMasterVersion({
          serviceId: row.serviceId,
          serviceVersion: row.serviceVersion,
        });
        if (res.code === "200") {
          this.$message.success("设置主版本成功！");
          this.row.serviceMasterVersion = row.serviceVersion
          this.getVersionList();
        }
      } else if (key === "discard") {
        const res = await discardService({
          serviceId: row.serviceId,
          serviceVersion: row.serviceVersion,
        });
        if (res.code === "200") {
          this.$message.success("作废成功！");
          this.getVersionList();
        }

      } else if (key === "upgrade") {
        this.$emit("upgradeVersion", row );
      } else if (key === "view") {
        this.$emit("viewVersion", row );
      } else if (key === "edit") {
        this.$emit("editVersion", row );
      }
    },
    actionBtnHandler(status) {
      // 驳回、暂存、作废
      const arr = ["1", "7", "9"];
      if (arr.includes(status)) {
        return true;
      }
      return false;
    },
  },
};
</script>

<style lang="scss" scoped>
:deep( .el-form-item) {
  margin-bottom: 0px;
}
</style>
